home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / SEARCH.C < prev    next >
C/C++ Source or Header  |  1997-07-30  |  3KB  |  133 lines

  1. #ifdef DEMO
  2. #include <stdio.h>
  3. #include <string.h>
  4. #else
  5. #include "global.h"
  6. #include "commands.h"
  7. #include "hardware.h"
  8. #ifndef MSDOS
  9. #include "proc.h"
  10. #endif
  11. #endif
  12.  
  13. #if !defined(_lint)
  14. static char rcsid[] OPTIONAL = "$Id: search.c,v 1.15 1997/07/31 00:44:20 root Exp root $";
  15. #endif
  16.  
  17. static int searchfileFP (register char *searchfor,register FILE *fp,register char *buf,int entrysize, int *passes, int searchlen);
  18.  
  19.  
  20. static int searchfileFP (searchfor, fp, buf, entrysize, passes, searchlen)
  21. register char *searchfor;
  22. register FILE *fp;
  23. register char *buf;
  24. int entrysize, *passes, searchlen;
  25. {
  26. register int low = 0, high;
  27. long size;
  28. register int start;
  29. register int result, found = 0;
  30. int theindex = 0;
  31. char *cp, origchar = 0;
  32.  
  33.     if (passes)
  34.         *passes = 0;
  35.     if (!searchlen)
  36.         searchlen = (int) strlen (searchfor);
  37.     size = (long) filelength (fileno(fp));
  38.     if (size < entrysize)
  39.         return (-1);
  40.     size /= entrysize;
  41.     high = size - 1;
  42.     theindex = start = size / 2;
  43.     for ( ; ; )    {
  44.         kwait (NULL);
  45.         fseek (fp, (long) ((long)theindex * (long)entrysize), 0);
  46.         (void) fread (buf, 1, (unsigned)entrysize, fp);
  47.         if (feof(fp))
  48.             continue;
  49.         if (passes)
  50.             (*passes)++;
  51.         cp = strpbrk (buf, ".@ \t");
  52.         if (cp)    {
  53.             origchar = *cp;
  54.             *cp = 0;
  55.         }
  56.         result = strnicmp (searchfor, buf, (unsigned)searchlen);
  57.         if (!result)    {
  58.             if (searchlen == (int)strlen(buf))    {
  59.                 found = 1;
  60.                 if (cp)
  61.                     *cp = origchar;
  62.                 break;
  63.             }
  64.         }
  65.         if (start != 1)
  66.             start /= 2;
  67.         if (result < 0)    {
  68.             high = theindex - 1;
  69.             theindex -= start;
  70.         } else    {
  71.             low = theindex + 1;
  72.             theindex += start;
  73.         }
  74.         if (theindex < low || theindex > high)
  75.             break;
  76.     }
  77.     if (found)    {        /* refresh it */
  78.         fseek (fp, (long) ((long)theindex * (long)entrysize), 0);
  79.         (void) fread (buf, 1, (unsigned) entrysize, fp);
  80.     }
  81.     return ((found) ? theindex : -1);
  82. }
  83.  
  84.  
  85. int searchfile (searchfor, fname, buf, entrysize, passes, searchlen)
  86. register char *searchfor;
  87. const char *fname;
  88. register char *buf;
  89. int entrysize, *passes, searchlen;
  90. {
  91. int result;
  92. FILE *fp;
  93.  
  94.     if ((fp = fopen (fname, "rb")) == NULL)
  95.         return (-1);
  96.     result = searchfileFP (searchfor, fp, buf, entrysize, passes, searchlen);
  97.     (void) fclose (fp);
  98.     return (result);    
  99. }
  100.  
  101.  
  102. #ifdef DEMO
  103. #define ENTRYSIZE 49
  104.  
  105. int
  106. kwait (i)
  107. int i;
  108. {
  109.     return i;
  110. }
  111.  
  112.  
  113. void
  114. main (argc, argv)
  115. int argc;
  116. char *argv[];
  117. {
  118. int found, passes;
  119. char buf[128], *searchfor;
  120.  
  121.     searchfor = "kp4djt";
  122.     if (argc > 1)
  123.         searchfor = argv[1];
  124.     found = searchfile (searchfor, "/nos/spool/wpagebbs", buf, ENTRYSIZE, &passes);
  125.  
  126.     printf ("String '%s' %sfound - %d passes\n", searchfor, (found == -1) ? "NOT " : "", passes);
  127.     if (found != -1)    {
  128.         buf[ENTRYSIZE] = 0;
  129.         puts (buf);
  130.     }
  131. }
  132. #endif
  133.